<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('googlemaps');
		$config = array('center'=>"51.166045, 4.136864",'zoom'=>17,'map_height'=>'400','map_type'=>'ROADMAP','scrollwheel'=> false);
		
		$marker = array();
		$marker['position'] = "51.165845, 4.137264";
		$marker['title'] = "You Studio";
		$marker['icon'] = base_url('../resources/common/imgs/mapmarker.png');
		$marker['infowindow_content'] = "YUHUA SHOP | Sint-Niklaas";
		
		$this->googlemaps->initialize($config);
		$this->googlemaps->add_marker($marker);
	}
	
	
	public function index()
	{
		if($_POST){
			$this->form_validation->set_rules('name','naam','required');
			$this->form_validation->set_rules('telephone','telefoon','required');
			$this->form_validation->set_rules('email','email','required|valid_email');
			$this->form_validation->set_error_delimiters('<div class="text-pink">', '</div>');
			if($this->form_validation->run()==false){
				
			}else{
				$mailData = $_POST;
				$email = $this->send_email($mailData);
				if($email){
							$data = array('value'=>'succeed');
							$this->session->set_flashdata('value',$data);
							redirect('contact/result');
				}else{
					$data = array('value'=>'failed');
							$this->session->set_flashdata('value',$data);
							redirect('contact/result');
				}
			}
		}
		$data['title'] = $this->title('Contact');
		$data['map'] = $this->googlemaps->create_map();
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/contact/index');
		$this->load->view($this->theme.'/footer');
			
	}
	
	public function result()
	{
		$result = $this->session->flashdata('value');
		if(!$result){
			redirect('contact');
		}else{
		$data['r'] = $this->session->flashdata('value');
		
			
		$data['title'] = $this->title('Contact');
		$data['map'] = $this->googlemaps->create_map();
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/contact/result');
		$this->load->view($this->theme.'/footer');
		}
	}
	
	private function send_email($mailData)
	{

		$data['mailData'] = $mailData;
		$config['protocol'] = 'mail';
		$config['wordwrap'] = FALSE;
		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['crlf'] = "\r\n";
		$config['newline'] = "\r\n";
		$this->email->initialize($config);
		$this->email->from($_POST['email'], $_POST['email']);
		$this->email->to('info@betalife.be');
		$this->email->subject('Nieuw Message | Yuhuashop');

		$email = $this->load->view($this->theme.'/emails/admin',$data,TRUE);
		$this->email->message($email);
		if($this->email->send()){
			return TRUE;
		}else{
			return FALSE;
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */